<?php
declare (strict_types=1);

namespace app\command;

use app\model\topic\TopicModel;
use app\model\topic\TopicNewModel;
use app\service\utils\TopicService;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class Hello extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('hello')->setDescription('更新题库 每周执行一次');
    }

    protected function execute(Input $input, Output $output)
    {
        $info = TopicNewModel::where('id', '>', 0)->count();
        if (!$info) {
            $output->writeln('新表没有数据，暂停执行');
            return;
        }
        // 备份表
        $backup = "topic_" . date('YmdHis');
        Db::execute("CREATE TABLE " . $backup . " LIKE topic;");
        Db::execute("INSERT INTO " . $backup . " SELECT * FROM topic;");

        // 清空原来的表
        Db::execute("TRUNCATE TABLE mp_topic;");

        $sql = "INSERT INTO topic SELECT * FROM topic_new;";
        Db::execute($sql);

        $arr = ['C1', 'A1', 'D', 'A2'];
        $arr1 = [1, 4];
        $topicService = new TopicService();
        foreach ($arr as $value) {
            foreach ($arr1 as $v) {
                $data = $topicService->getTopicList($value, $v);
                $total = $data['total'];
                $limit = 100;
                $totalPage = ceil($total / $limit);
                for ($i = 1; $i <= $totalPage; $i++) {
                    $result = $topicService->getTopicList($value, $v, '', $i, $limit);
                    $list = $result['list'];
                    $list1 = [];
                    foreach ($list as $key => $val) {
                        $val['subject'] = $v;
                        $list1[$key] = $val;
                    }
                    $topicModel = new TopicNewModel();
                    $topicModel->saveAll($list1);
                    sleep(2);
                }
            }
        }
        $output->writeln('hello');
    }
}
